<?php
/***
 * 品牌后台控制器
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */


namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Services\BrandService\BrandBackService;
use App\Services\BrandService\BrandToolService;
use App\Services\ImgServices\ImgTool;
use App\Services\UserService\UserTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class BrandController extends Controller
{
    protected $brandBackSer;
    protected $brandToolSer;
    protected $imgTool;
    /**
     * BrandController constructor.
     */
    public function __construct(BrandBackService $brandBackSer,
                                BrandToolService $brandToolSer,
                                ImgTool $imgTool)
    {
        $this->brandBackSer=$brandBackSer;
        $this->brandToolSer=$brandToolSer;
        $this->imgTool=$imgTool;
    }

    /**
     * 添加品牌页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAddBrand(Request $request)
    {
        return view('backstage.brand.addBrand');
    }

    /***
     * 保存品牌
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postAddBrand(Request $request)
    {
        $brand_name = trim($request->input('brand_name'));
        $logo = $request->file('logo');
        $description = trim($request->input('description'));
        $brand =  array('brand_name'=>$brand_name,'logo'=>$logo,'description'=>$description);
        $validator =  $this->brandBackSer->addValidator($brand);
        unset($brand['logo']);
        $validator->after(function($validator) use ($brand_name)
        {
            //判断除当前品牌名称外有没有重复品牌名称
            $b =  $this->brandBackSer->getBrandForNameIsNotCurrent('',$brand_name);
            if(!empty($b))
            {
                $validator->errors()->add('brand_name', '品牌名称已存在!');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();;
        }
        if(!empty($logo))
        {
            $img_path=$this->imgTool->imgSave($logo);
            $brand['logo']=empty($img_path)?'':$img_path;
        }
        $res=$this->brandBackSer->saveBrand($brand);
        if($res){
            return redirect()->back()->with('success_message','添加成功');
        }
        return redirect()->back()->with('error_message','添加失败');
    }
    /**
     * 获取品牌列表
     * @param Request $request
     * @return mixed
     */
    public function postSearchBrandList(Request $request){
        $brands =  $this->brandToolSer->getSearchBrandList($request);
        $result=array('data'=>$brands,'state'=>1,'message'=>'获取成功');
        return response()->json($result);
    }

    /**
     * 获取品牌列表
     * @param Request $request
     * @return $this
     */
    public function getBrandList(Request $request){
        $data['per_page']=10;
        $brands =  $this->brandBackSer->getBrandList($data);
        foreach ($brands as $brand){
            $brand->logo=ImgTool::getImgUrlByImgPath($brand->logo);
        }
        return view('backstage.brand.brandList')->with('brands',$brands);
    }

    /**
     * 删除品牌
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postDeleteBrand(Request $request)
    {
        $brand_id = trim($request->input("brand_id"));
        $brand_exist=BrandToolService::brandExistByBrandId($brand_id);
        if(!$brand_exist){
            $data['state']=-2;
            $data['message']='品牌不存在！';
            return response()->json($data);
        }
        $del_res =  $this->brandBackSer->deleteBrand($brand_id);
        if($del_res){
            $data['state']=1;
            $data['message']='删除成功！';
        }else{
            $data['state']=-1;
            $data['message']='删除失败！';
        }
        return response()->json($data);
    }

    /**
     * 品牌更新页面
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getUpdateBrand(Request $request)
    {
        $brand_id = trim($request->input("brand_id"));
        $brand_exist=BrandToolService::brandExistByBrandId($brand_id);
        if($brand_exist===false){
            return redirect()->back()->with('error_message', '品牌不存在');
        }
        $brand=$this->brandToolSer->getBrandByBrandId($brand_id);
        if(empty($brand)){
            return redirect()->back()->with('error_message', '品牌不存在');
        }
        $brand->logo=ImgTool::getImgUrlByImgPath($brand->logo);
        return view('backstage.brand.updateBrand')->with('brand',$brand);
    }
    /**
     * 品牌更新页面
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postUpdateBrand(Request $request)
    {
        $brand_id = trim($request->input("brand_id"));
        $brand_exist=BrandToolService::brandExistByBrandId($brand_id);
        if($brand_exist===false){
            return redirect()->back()->with('error_message', '品牌不存在');
        }
        $logo=$request->file('logo');
        $description = trim($request->input("description"));
        $brand_name = trim($request->input("brand_name"));
        $data['brand_id']=$brand_id;
        $data['description']=$description;
        $data['brand_name']=$brand_name;
        if(!empty($logo)){
            $img_url=$this->imgTool->imgSave($logo);
            if(!empty($img_url)){
                $data['logo']=$img_url;
            }
        }
        $update_res=$this->brandBackSer->updateBrand($data);
        if($update_res)
        {
            return redirect()->back()->with('success_message', '添加成功');
        }else{
            return redirect()->back()->with('error_message', '添加失败');
        }
    }
}